package com.karson.common.config.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

/**
 * @author ZKS
 */
@Configuration
public class RedisConfig {
    public static final String COUNT_KEY = "count:";
    public static final String TAG_COUNT_KEY = "count:tag:";
    public static final String ALBUM_USER_LIKE = "like:user:";
    public static final String ALBUM_USER_DISLIKE = "dislike:user:";
    public static final String ALBUM_USER_PLAY = "play:user:";
    public static final String ALBUM_USER_FAV = "fav:user:";
    public static final String HASH_LIKE_KEY = "like";
    public static final String HASH_DISLIKE_KEY = "dislike";
    public static final String HASH_FAV_KEY = "fav";
    public static final String HASH_PLAY_KEY = "play";
    public static final String ZSET_FAV_KEY = "favorite:";
    public static final String FAV_GLOBAL_SEQ_KEY = "favorite:incr";
    public static final String HASH_CLICK_KEY = "pv";
    public static final String USER_WEIGHT_KEY = "user:weight:";
    public static final String USER_RECOMMEND_KEY = "user:recommend:";
    public static final String TRACK_PLAY = "track:play:";
    public static final String IS_COMPUTED = "computed:";
    /** 时间维度热门内容 */
    public static final String HOT_ALBUMS = "hot:albums:";
    public static final String HOT_RANK = "hot:rank";
    /** 个性化推荐 */
    public static final String INTEREST_RECOMMEND = "interest:recommend:";
    public static final String INTEREST_RECOMMEND_EXP = "interest:recommend:exp:";

    /** 系统库 */
    public static final String SYSTEM_STOCK = "system:stock:";
    public static final String SYSTEM_HISTORY_TEMP = "system:temp:";



    public static final long CACHE_TTL_SECONDS = 3600;
    /** 逻辑过期时间 1小时 */
    public static final long LOGICAL_EXPIRE_MILLIS = 60 * 60 * 1000L;


    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);

        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

}

